﻿using System.Windows;
using System.ComponentModel;

namespace MVVMExample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = App.ViewModel;
            App.ViewModel.Student.PropertyChanged += Student_PropertyChanged;
        }

        /// <summary>
        /// 该函数可以确保获取当前 Student 的属性值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_ShowMessage_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show($"{App.ViewModel.Student}");
        }

        /// <summary>
        /// 该事件用于通知界面上的所有属性发生了变化，以解决缺少 [NotifyPropertyChangedFor(nameof(Description))] 和绑定自身的问题
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_NotifyAll_Click(object sender, RoutedEventArgs e)
        {
            // 必须要先设置为 null，才会重新计算所有的属性
            DataContext = null;
            DataContext = App.ViewModel;
        }

        /// <summary>
        /// 当 Student 的属性发生变化时，将会触发此事件，仅能从参数中获取属性名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Student_PropertyChanged(object? sender, PropertyChangedEventArgs e)
        {
            TextBox_Log.AppendText($"{e.PropertyName} has changed.{Environment.NewLine}");
        }

        private void Button_SetTrue_Click(object sender, RoutedEventArgs e)
        {
            App.ViewModel.ListOfBoolean[1] = true;
        }

        private void Button_SetSelected_Click(object sender, RoutedEventArgs e)
        {
            foreach (Student _item in App.ViewModel.ListOfStudents)
            {
                _item.IsSelected = true;
            }
        }
    }
}